package com.ctshk.common.utils;

import cn.hutool.core.util.RandomUtil;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author 谢诗宏
 * @description 系统单号规则
 * @date 2021/5/7
 */
public class NumberUtil {

    /**
     * 生成酒店产品编号号
     * PH+員工編號+月日4位+時分4位+4位隨機數字
     * @param employeeNumber
     * @return
     */
    public static String generatorHotelProductNumber(String employeeNumber) {
        String dateCode = DateUtil.fmtNum(LocalDate.now());
        String randomNumber = RandomUtil.randomNumbers(4);
        StringBuilder numberSb = new StringBuilder();
        numberSb.append("PH").append(employeeNumber).append(dateCode).append(randomNumber);
        return numberSb.toString();
    }

    /**
     * 生成酒店订单号
     * H+deptCode(部门code)+employeeNumber(员工号)+年後2位+年月日6位+3位隨機數字
     * @param deptCode
     * @param employeeNumber
     * @return
     */
    public static String generatorOrderHotelNumber(String deptCode, String employeeNumber) {
        String dateCode = DateUtil.fmtNum(LocalDate.now());
        String randomNumber = RandomUtil.randomNumbers(3);
        StringBuilder numberSb = new StringBuilder();
        numberSb.append("H").append(deptCode).append(employeeNumber).append(dateCode).append(randomNumber);
        return numberSb.toString();
    }

    /**
     * 退款编号生成器，规则：TKD+年後2位+月日4位+時分秒6位+2位隨機數字
     *
     * @param nowLocalDateTime
     * @return
     */
    public static String generateOrderHotelRefundNumber(LocalDateTime nowLocalDateTime) {
        String dateCode = DateUtil.fmtYyMMddHHmmss(nowLocalDateTime);
        String randomNumber = RandomUtil.randomNumbers(2);
        StringBuilder numberSb = new StringBuilder();
        numberSb.append("TKD")
                .append(dateCode)
                .append(randomNumber);
        return numberSb.toString();
    }

    /**
     * 生成收款单号:
     * SKD+年後2位+月日4位+時分秒6位+2位隨機數字
     */
    public static String generatorPaymentNumber(LocalDateTime nowLocalDateTime) {
        String dateCode = DateUtil.fmtYyMMddHHmmss(nowLocalDateTime);
        String randomNumber = RandomUtil.randomNumbers(2);
        StringBuilder orderNumberSb = new StringBuilder();
        orderNumberSb.append("SKD")
                .append(dateCode)
                .append(randomNumber);
        return orderNumberSb.toString().toUpperCase();
    }

    /**
     * 生成收據號碼：門市編號+員工編號+年後2位+月日4位+B+時分秒6位+（S），
     * 例如：XS-C07884-200407-B-110008（S）
     *
     * @return
     */
    public static String generatorReceiptNumber(String deptCode, String employeeNumber, LocalDateTime
            nowLocalDateTime) {
        String symbol = "-";
        String dateCode = DateUtil.fmtNum(LocalDate.now());
        String hourCode = StringUtils.leftPad(String.valueOf(nowLocalDateTime.getHour()), 2, "0");
        String minuteCode = StringUtils.leftPad(String.valueOf(nowLocalDateTime.getMinute()), 2, "0");
        String secondCode = StringUtils.leftPad(String.valueOf(nowLocalDateTime.getSecond()), 2, "0");
        StringBuilder receiptNumberSb = new StringBuilder();
        receiptNumberSb.append("XS").append(symbol).append(deptCode).append(symbol)
                .append(employeeNumber).append(symbol)
                .append(dateCode).append(symbol).append("-B-")
                .append(hourCode).append(symbol)
                .append(minuteCode).append(symbol)
                .append(secondCode).append(symbol)
                .append("（S）");
        return receiptNumberSb.toString().toUpperCase();
    }

}
